package com.hfdg.organization.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfdg.prototype.base.utils.TimeUtils;
import com.hfdg.organization.mapper.ClerkMapper;
import com.hfdg.organization.entity.Clerk;
import com.hfdg.organization.service.ClerkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName ClerkServiceImpl
 * @Description TODO
 * @Author yuelh
 * @Date 2019/1/19 18:26
 * @Version 1.0
 * @Company 会飞的锅工作室
 **/
@Service
public class ClerkServiceImpl implements ClerkService {
    @Autowired
    ClerkMapper clerkMapper;

    @Override
    public String insert(Clerk clerk) {
        String uid = UUID.randomUUID().toString();
        clerk.setUid(uid);
        clerk.setDelFlag("0");
        clerk.setCreateTime(new Date());
        clerkMapper.insert(clerk);
        return uid;
    }

    @Override
    public Integer update(Clerk clerk) {
        clerk.setUpdateTime(new Date());
        return clerkMapper.update(clerk);
    }

    @Override
    public Integer  delete(String uid) {
        return clerkMapper.delete(uid);
    }
    @Override
    public Integer batchDelete(String[] uids) {
            return clerkMapper.batchDelete(uids);
    }

    @Override
    public Clerk get(String uid) {
        return clerkMapper.get(uid);
    }

    @Override
    public List<Clerk> list(Clerk clerk) {
    return  clerkMapper.list(clerk);
    }

    @Override
    public PageInfo<Clerk> getPagingData(int offset, int limit, String orderBy, Clerk clerk) {
        if(null==orderBy|| StringUtils.isBlank(orderBy)){
        PageHelper.startPage((offset/limit)+1,limit);
        }else{
        PageHelper.startPage((offset/limit)+1,limit,orderBy);
        }
        PageInfo<Clerk> pageInfo =new PageInfo<>(clerkMapper.list(clerk));
            return pageInfo;
            }
    @Override
    public String insertOrUpdate(Clerk clerk){
        if(null!=clerk.getUid()){
            this.update(clerk);
        }else{
            this.insert(clerk);
        }
        return clerk.getUid();
    }

    @Override
    public List<Map<String, Object>> listDepartmentTree(Clerk clerk) {
        return clerkMapper.listDepartmentTree(clerk);
    }

    @Override
    public List<Map<String, Object>> listPositionTree(Clerk clerk) {
        return clerkMapper.listPositionTree(clerk);
    }

    @Override
    public List<Map<String, Object>> listStationTree(Clerk clerk) {
        return clerkMapper.listStationTree(clerk);
    }
}
